$(function(){

    var stockRecordPopUp = com.focusPage('#stockRecordPopUp');

    var displayFunc  = (function(){
        /*表格样式*/
        var renderList = function(tableId,listData){
            var $tableWrap = com.focusPage('#' + tableId);

            if(listData && listData.length){
                var $ulTitle = createTableTitle();
                var $ulData = $('<ul></ul>').addClass('flex-table');
                $tableWrap.append($ulTitle).append($ulData);

                $.each(listData,function(i, v){
                    var $liLine = creatLine(i, v, i == listData.length - 1);

                    $liLine.appendTo($ulTitle);
                });
                $('#resultListstockRecord').css({'width':'96%','margin':'0 auto','height':'560px','marigin-top':'20px'})
            }
        };

        /*表格头*/
        var createTableTitle = function(){
            var $ulTitle = $('<ul></ul>').addClass('flex-table');
            var $liTitle = $('<li></li>').addClass('line table-title');

            var $indexWrap = $('<div></div>').addClass('line-wrap order').html($('<div></div>').addClass('cell index'));
            var $contentWrap = $('<div></div>').addClass('line-wrap  content col8');

            var titleName = '奖品,类型,数量,总价,单价,提交人,时间,说明';

            $.each(titleName.split(','), function (i, v) {
                var $cell = $('<div></div>').addClass('cell').html(v);
                $cell.appendTo($contentWrap);
            });
            $liTitle.append($indexWrap).append($contentWrap).appendTo($ulTitle);
            return $ulTitle;
        };

        /*表格体*/
        var creatLine = function (i, v, lastFlag) {
            var $liLine = $('<li></li>').addClass('line');
            var $indexWrap = $('<div></div>').addClass('line-wrap order').html($('<div></div>').addClass('cell index').html(i + 1));
            var $contentWrap = $('<div></div>').addClass('line-wrap  content col8');
            var type = {
                "0": "入库",
                "1": "出库",
                "2": "盘盈",
                "3": "盘亏"
            };
            var propertyArr = [
                [v.awardStock.name,'grey'],
                [type[v.type],'grey'],
                [v.num,'grey'],
                [v.totalPrice,'grey'],
                [v.unitPrice, 'grey'],
                [v.creater.currentName,'grey'],
                [v.operateTime,'red'],
                [v.content,'grey']];

            $.each(propertyArr, function (i, v) {
                var $cell = $('<div></div>').addClass('cell ' + v[1]).html(v[0]);
                $cell.appendTo($contentWrap);
            });


            $liLine.append($indexWrap).append($contentWrap);

            return $liLine;
        };

        return {
            init : function(tableId,listData){
                com.focusPage('#' + tableId).html(null);
                renderList(tableId,listData);

            },
            drawLine : function(v){
                return creatLine(v);
            }
        }
    })();

    doSearcher();

    function doSearcher() {
        com.post(api.information.queryAwardByAsId, {"awardRecord.awardStock.asId": stockRecordPopUp.attr("data-asid")}, function (res) {
            console.log(res)
            console.log(res.stockrecords);
            displayFunc.init('resultListstockRecord', res.awardRecords);
        });
    }

    function addline(lineindex) {
        var $line = $('<li></li>').addClass('line');
        var $indexWrap = $('<div></div>').addClass('line-wrap order').append($('<div></div>').addClass('cell index').text(lineindex));
        var $lineInfoWrap = $('<div></div>').addClass('line-wrap content col1');
        var _lineInfoSet = [{cellClass : "info", hasInput : !0}];
        var $btncheck = $('<a class="color-green green" style="font-size: 18px"><i class="icon-ok"></i></a>');
        var $btndel = $('<a class="color-red red" style="font-size: 18px"><i class=" icon-remove"></i></a>');

        $.each(_lineInfoSet, function (i, v) {
            var $input;
            var $cell = $('<div></div>').addClass('cell ' + v.cellClass);
            if (v.hasInput) {
                $input = $('<input>').prop({'type': 'text', 'autocomplete': 'off'}).addClass('editable v_required');
                $cell.append($input);
            }
            $cell.appendTo($lineInfoWrap);
        });

        $btndel.click(function (e) {
            com.focusPage('#list').empty();
            addline(1).addClass('current').appendTo('#list');
        });
        return $line;
    }

    addline(1).addClass('current').appendTo('#list');

    com.focusPage('.layui-layer-content').css('overflow', 'hidden');
    com.focusPage('#showstockRecord').css('height', '680px');
    com.focusPage('#showstockRecord').css('overflow-y', 'scroll');

});
